//
//  RdioListViewController.m
//  Leisure
//
//  Created by xalo on 16/7/2.
//  Copyright © 2016年 朱旭东. All rights reserved.
//

#import "RadioListViewController.h"

@interface RadioListViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSMutableArray *dataSourceArray;

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property(nonatomic,strong)UIImageView *imageView;
@property(nonatomic,strong)NSArray *listArray;
@property(nonatomic,assign)NSInteger Start;//列表数据起始位置
@property(nonatomic,assign)NSInteger limit;//列表的条数

@end

@implementation RadioListViewController



- (NSMutableArray *)dataSourceArray
{
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets  = NO;
    self.view.backgroundColor = [UIColor  grayColor];
    [self.tableView  registerNib:[UINib nibWithNibName:@"RadioListCell" bundle:nil] forCellReuseIdentifier:@"RadioList_cell"];
    
    //数据请求
    [RequestManager fetchDataWithURL:kRadioListUrl parameters:@{@"radioid":self.radioid} method:@"POST" complet:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];

//        NSLog(@"%@",dic);
        for (NSDictionary *sunDic in dic[@"data"][@"list"]) {
            RadioListModel *model = [[RadioListModel alloc] init];
            [model  setValuesForKeysWithDictionary:sunDic];
            [model setValuesForKeysWithDictionary:sunDic[@"playInfo"]];
            [model setValuesForKeysWithDictionary:sunDic[@"playInfo"][@"authorinfo"]];
            [self.dataSourceArray addObject:model];
            NSLog(@"%@",model.webview_url);
            
            
            //前面的 背景图
            self.imageView = [[UIImageView  alloc] initWithFrame:CGRectMake(0, 64, kwidth, kheight/3)];
            [self.imageView  sd_setImageWithURL:[NSURL URLWithString:model.coverimg]];
           
        }
        
    } updataUI:^{
        [self.tableView reloadData];
        self.tableView.tableHeaderView = self.imageView;
    }];

    //下拉刷新
    self.tableView.mj_header = [MJRefreshStateHeader headerWithRefreshingBlock:^{
        //刷新之前清空数组
        [self.dataSourceArray removeAllObjects];
        self.Start= 0;
        [self  loadTableViewDataSourceWithStart:0 limit:10];
    }];
    
    //添加上拉加载,执行完之后要结束
    self.tableView.mj_footer = [MJRefreshBackGifFooter  footerWithRefreshingBlock:^{
        self.Start += 10;
        [self  loadTableViewDataSourceWithStart:self.Start limit:10];
    }];

}
#pragma mark -----------加载数据---------------
- (void)loadTableViewDataSourceWithStart:(NSInteger)start limit:(NSInteger)limit{
    [RequestManager fetchDataWithURL:kloadUpDown parameters:@{} method:@"POST" complet:^(NSData *data) {
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        NSLog(@"%@",dic);
        for (NSDictionary *subdic in dic[@"data"][@"list"]) {
            RadioListModel *model = [[RadioListModel alloc]init];
            [model  setValuesForKeysWithDictionary:subdic];
            [self.dataSourceArray addObject:model];
        }
        
    } updataUI:^{
        [self.tableView.mj_footer  endRefreshing];
        [self.tableView.mj_header endRefreshing];
        [self.tableView reloadData];
    }];
}

#pragma mark ----------------tableViewDelegate-------------------

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RadioListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RadioList_cell" forIndexPath:indexPath];
    RadioListModel *model = self.dataSourceArray[indexPath.row];
    
    cell.playBlock = ^(){
        RadioPlay_info_ViewController *listVC = [RadioPlay_info_ViewController defaultManager];
        listVC.index = indexPath.row;
        listVC.dataSource = self.dataSourceArray;
        [self.navigationController pushViewController:listVC animated:YES];
    };
    [cell setCellWithModel:model];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArray.count;

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 100;
}



@end
